/*
 * @author: Patrick Black
 */

package Model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.swing.JTable;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import FileParser.FileParser;
import TableModels.SPCTemplate;
import TableModels.ReviewerTemplate;

public class Reviewer extends User {

	private final static Integer MAX_PAPERS = 4;
	
	private String name;
	private Conference conference;	
	private String email;
	private Integer phone;

	private FileParser parser = new FileParser();
	ArrayList<Manuscript> manuscripts = new ArrayList<Manuscript>();
	
	
	/**
	 * 
	 * @param a_fp
	 * @param a_name
	 * @param a_conference
	 * @param a_email
	 * @param a_phone
	 */
	public Reviewer(FileParser a_fp, String a_name, Conference a_conference) {
		super(a_fp, a_name, a_conference);
		name= a_name;
		conference = a_conference;
		parser = a_fp;
		manuscripts = a_fp.getManuscripts(Roles.REVIEWER.toString(), a_name, a_conference.getName());
	}
	public String viewManuscript(final JTable table) {
		String manuscript = "";
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
							//must cast to correct TableModel, or getManuscriptLink wont work.

				
			String link = ((ReviewerTemplate)table.getModel()).getManuscriptLink(row);//get link	
			InputStream stream = FileParser.class.getResourceAsStream(link);	//convert to stream
			try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
					XWPFDocument docx = new XWPFDocument(stream);				//parse docx
					XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
					manuscript = we.getText();				
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
					stream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return manuscript;
	}
	
	/**
	 * 
	 * @return manuscripts field
	 */
	public ArrayList<Manuscript> getManuscripts() {
		return manuscripts;
	}


	/**
	 * finds requested paper and changes review link to passed link.
	 * @param a_manuscript paper to review
	 * @param a_link link of new review
	 * @return confirmation message
	 */
	public String submitReview(final Manuscript a_manuscript, final String a_link) {
		String confirmation = "Deadline exceeded. No changes made.";
		int paper = findPaper(a_manuscript);
		if(paper == -1) {
			confirmation = "Manuscript not found. No changes made";
			return confirmation;
		}

		if(getDate().before(conference.getDeadlineDate())) {
			
			Manuscript temp = manuscripts.get(paper);
			ArrayList<Review> reviews = temp.getReviews();
			Review tempReview;
			for(int i = 0; i < reviews.size(); i++) {
				
				if (reviews.get(i).getReviewer().equals(name)) {
			
					tempReview = reviews.get(i);
					tempReview.setReviewLink(a_link);
					reviews.set(i, tempReview);
					temp.setReviews(reviews);
					
					
					
					parser.saveManuscript(a_manuscript.getTitle(), temp);
					manuscripts = parser.getManuscripts(Roles.REVIEWER.toString(), name, conference.getName());
					
					confirmation = "Review updated.";
				}
			}
		}
		return confirmation;
	}
	
	public ArrayList<String> viewReview(final JTable table){
		ArrayList<String> review = new ArrayList<String>();
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
							//must cast to correct TableModel, or getManuscriptLink wont work.

				
			//String link = ((TemplateTableModel)table.getModel()).getManuscriptLink(row);//get link	
			ArrayList<Review> links = ((SPCTemplate)table.getModel()).getReviews(row);//get link
			if(!links.isEmpty()) {
				for(int i = 0; i < links.size(); i++) {
					InputStream stream = FileParser.class.getResourceAsStream(links.get(i).getReviewLink());	//convert to stream
					try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
							XWPFDocument docx = new XWPFDocument(stream);				//parse docx
							XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
							review.add(i, we.getText());						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
							stream.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}
		System.out.println(review.size());
		return review;
	}

	
	/**
	 * 
	 * @param a_manuscript requested paper
	 * @return index of paper inside manuscripts list, -1 if not found.
	 */
	private int findPaper(final Manuscript a_manuscript) {
		int paper = -1;
		for(int j = 0; j < manuscripts.size(); j++) {
			if (manuscripts.get(j).getTitle().equals(a_manuscript.getTitle())){
				paper = j; //manuscript locator
			}
		}
		return paper;
	}
	
	
	/**
	 * checks to see if a paper with the same title and link already exist in memory.
	 * @param a_title title of new paper
	 * @param a_link link to new paper
	 * @return true if exist false if not.
	 */
	private boolean alreadyExists(final String a_title, final String a_link) {
		boolean exists = false;
		for(int i = 0; i < manuscripts.size(); i++) {
			if (manuscripts.get(i).getTitle().equals(a_title) &&
					manuscripts.get(i).getLink().equals(a_link)) {
					exists = true;
				}
			}
		return exists;
	}
	
	
	

}
